package com.woniu.carrent.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.carrent.mapper.TOrderMapper;
import com.woniu.carrent.pojo.TOrder;
import com.woniu.carrent.pojo.dto.TOrderDto;
import com.woniu.carrent.pojo.vo.TOderVo;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@SaCheckLogin
@RestController
@RequestMapping("/torder")
@CrossOrigin
public class TOrderController {
    @Autowired
    private TOrderMapper tOrderMapper;
    /**
     * 查询订单
     * @param torderDto
     * @return
     */
    @RequestMapping("/slectOrder")
    public ResultMessage slectOrder(@RequestBody TOrderDto torderDto){
        TOrder tOrder = new TOrder();
        BeanUtils.copyProperties(torderDto, tOrder);
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        if(torderDto.getUserid() != null){
            wrapper.eq("o.userId", torderDto.getUserid());
        }
        if(torderDto.getCdid() != null){
            wrapper.eq("o.cdId", torderDto.getCdid());
        }
        if(torderDto.getIspaid() != null){
            wrapper.eq("o.isPaid", torderDto.getIspaid());
        }
        // 精确时间查询
        if (torderDto.getCreattime() != null){
            wrapper.eq("o.creatTime", torderDto.getCreattime());
        }
        if (torderDto.getEndtime() != null){
            wrapper.eq("o.endTime", torderDto.getEndtime());
        }
        // 时间范围查询
        if (torderDto.getStartTime() != null && torderDto.getEndTime() != null) {
            // 在开始时间和结束时间之间
            wrapper.between("o.creatTime", torderDto.getStartTime(), torderDto.getEndTime());
        } else {
            if (torderDto.getStartTime() != null) {
                // 大于等于开始时间
                wrapper.ge("o.creatTime", torderDto.getStartTime());
            }
            if (torderDto.getEndTime() != null) {
                // 小于等于结束时间
                wrapper.le("o.creatTime", torderDto.getEndTime());
            }
        }
        List<TOrder> list = tOrderMapper.selectAllByUseridTOrderList(wrapper);
        return ResultMessage.success(list);
    }

    @RequestMapping("/insertOrder")
    public ResultMessage insertOrder(@RequestBody TOrderDto torderDto){
        boolean flag = tOrderMapper.insert(torderDto) > 0;
        if (flag){
            return ResultMessage.success("添加成功");
        }
        return ResultMessage.fail("添加失败");
    }
    /**
     * 修改订单
     * @param torderDto
     * @return
     */
    @RequestMapping("/updateOrder")
    public ResultMessage updateOrder(@RequestBody TOrderDto torderDto){
        boolean flag = tOrderMapper.updateById(torderDto) > 0;
        if (flag){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 删除订单
     * @param torderDto
     * @return
     */
    @RequestMapping("/deleteOrder")
    public ResultMessage deleteOrder(@RequestBody TOrderDto torderDto){
        boolean flag = tOrderMapper.deleteById(torderDto) > 0;
        if (flag){
            return ResultMessage.success("删除成功");
        }
        return ResultMessage.fail("删除失败");
    }
    /**
     * 车行查询订单
     * @param torderDto
     */
    @RequestMapping("/selectOrderByUid")
    public ResultMessage selectOrderByUid(@RequestBody TOrderDto torderDto){
        Integer uid = torderDto.getUserid();
        List<TOderVo> list = tOrderMapper.selectOrderByUid(uid);
        return ResultMessage.success(list);
    }
}
